***Partisanship
gen Partisanship=. , before (I_1_Q69)
replace Partisanship=0 if Q65a==0
replace Partisanship=1 if Q65b==1
replace Partisanship=2 if Q65b==2 | Q65b==3 | Q65b==4 | Q65b==5 | Q65b==6 | Q65b==7 | Q65b==8 | Q65b==9 | Q65b==10 | Q65b==11

label define Partisanship1 0 "Nonpartisan" 1 "RDPC Partisan" 2 "Opposition Partisan"
label values Partisanship Partisanship1

gen Nonpartisanship=0 if Partisanship==0 , before (I_1_Q69)
replace Nonpartisanship=1 if Partisanship==1 | Partisanship==2
label define Nonpartisanship1 0 "Nonpartisan" 1 "Partisan"
label values Nonpartisanship Nonpartisanship1

gen Partisan=0 if Partisanship==1 , before (I_1_Q69)
replace Partisan=1 if Partisanship==2
label define Partisan1 0 "RDPC Partisan" 1 "Opposition Partisan"
label values Partisan Partisan1
label variable Partisan "Whether the respondent supports the RDPC or an opposition party"

gen OppositionPartisan=0 if Partisanship==0 | Partisanship==1, before (I_1_Q69)
replace OppositionPartisan=1 if Partisanship==2
label define Opposition1 0 "Nonpartisan or RDPC Partisan" 1 "Opposition Partisan"
label values OppositionPartisan Opposition1
label variable OppositionPartisan "Respondent feels close to an opposition party"

gen RulingPartyPartisan=0 if Partisanship==0 | Partisanship==2, before (I_1_Q69)
replace RulingPartyPartisan=1 if Partisanship==1
label define RulingPartyPartisan1 0 "Nonpartisan or Opposition partisan" 1 "Ruling party partisan"
label values RulingPartyPartisan RulingPartyPartisan1
label variable RulingPartyPartisan "Respondent feels close to the ruling party"

gen OppositionPartisan1=. , before (I_1_Q69)
replace OppositionPartisan1=0 if Partisanship==1
replace OppositionPartisan1=1 if Partisanship==2
label define Opposition2 0 "RDPC Partisan" 1 "Opposition Partisan"
label values OppositionPartisan1 Opposition2
label variable OppositionPartisan1 "Respondent feels close to an opposition party"

gen RulingPartyPartisan1=. , before (I_1_Q69)
replace RulingPartyPartisan1=0 if Partisanship==2
replace RulingPartyPartisan1=1 if Partisanship==1
label define RulingPartyPartisan2 0 "Opposition partisan" 1 "Ruling party partisan"
label values RulingPartyPartisan1 RulingPartyPartisan2
label variable RulingPartyPartisan1 "Respondent feels close to the ruling party"



************************Network Variables********************************
***Network Size
gen NetworkSize="." , before (Q76a)
replace NetworkSize="1" if I_1_ADDPERSON=="Terminer"
replace NetworkSize="0" if I_1_Q69=="Refused" | I_1_Q69=="Don't Know" 
replace NetworkSize="2" if I_2_ADDPERSON=="Terminer"
replace NetworkSize="3" if I_3_ADDPERSON=="Terminer"
replace NetworkSize="4" if I_4_ADDPERSON=="Terminer"
replace NetworkSize="5" if I_5_ADDPERSON=="Terminer"
replace NetworkSize="6" if I_6_ADDPERSON=="Terminer"
replace NetworkSize="7" if I_7_ADDPERSON=="Terminer"
destring NetworkSize, replace
label variable NetworkSize "Number of discussion partners mentioned"


***Average Age Partners Met
egen AverageAgeMet=rmean(I_1_Q70a I_2_Q70a I_3_Q70a I_4_Q70a I_5_Q70a I_6_Q70a I_7_Q70a)
label variable AverageAgeMet "Average age that the discussion partners met"
order AverageAgeMet, before (Q76a)

***Average Education
egen AverageEducation=rmean(I_1_Q70b I_2_Q70b I_3_Q70b I_4_Q70b I_5_Q70b I_6_Q70b I_7_Q70b)
label variable AverageEducation "Average level of education of all discussion partners"
order AverageEducation, before (Q76a)

***Average Discuss Politics
egen AverageDiscuss=rmean(I_1_Q70e I_2_Q70e I_3_Q70e I_4_Q70e I_5_Q70e I_6_Q70e I_7_Q70e)
label variable AverageDiscuss "Average amount of political discussion amongst all discussion partners"
order AverageDiscuss, before (Q76a)

***Average Political Interest
egen AverageInterest=rmean(I_1_Q70f I_2_Q70f I_3_Q70f I_4_Q70f I_5_Q70f I_6_Q70f I_7_Q70f)
label variable AverageInterest "Average level of political interest amongst all discussion partners"
order AverageInterest, before (Q76a)

***Average Agreement
egen AverageAgreement=rmean(I_1_Q70g I_2_Q70g I_3_Q70g I_4_Q70g I_5_Q70g I_6_Q70g I_7_Q70g)
label variable AverageAgreement "Average level of political agreement across all discussion partners"
order AverageAgreement, before (Q76a)

***Average R has influenced Network
egen AverageInfluencer=rmean(I_1_Q70h I_2_Q70h I_3_Q70h I_4_Q70h I_5_Q70h I_6_Q70h I_7_Q70h)
label variable AverageInfluencer "Average amount of political influence the respondent has across all discussion partners"
order AverageInfluencer, before (Q76a)

***Average R is influenced by Network
egen AverageInfluenced=rmean(I_1_Q70i I_2_Q70i I_3_Q70i I_4_Q70i I_5_Q70i I_6_Q70i I_7_Q70i)
label variable AverageInfluenced "Average amount the respondent has been influenced across all discussion partners"
order AverageInfluenced, before (Q76a)

***Average Community Influencer
egen AverageCommunity=rmean(I_1_Q70j I_2_Q70j I_3_Q70j I_4_Q70j I_5_Q70j I_6_Q70j I_7_Q70j)
label variable AverageCommunity "Average amount community influence across all discussion partners"
order AverageCommunity, before (Q76a)

***Average Partisanship
egen AveragePartisanship=rmean(I_1_Q70k I_2_Q70k I_3_Q70k I_4_Q70k I_5_Q70k I_6_Q70k I_7_Q70k)
label variable AveragePartisanship "Average amount partisanship/nonpartisanship across all discussion partners"
order AveragePartisanship, before (Q76a)

***Average Opposition conditional on partisanship
gen I_1_Q70l2=. , after(I_1_Q70l)
replace I_1_Q70l2=0 if I_1_Q70l==1
replace I_1_Q70l2=1 if I_1_Q70l==2 |  I_1_Q70l==3 |  I_1_Q70l==4 |  I_1_Q70l==5 |  I_1_Q70l==6 |  I_1_Q70l==7 |  I_1_Q70l==8 | I_1_Q70l==9 

gen I_2_Q70l2=. , after(I_2_Q70l)
replace I_2_Q70l2=0 if I_2_Q70l==1
replace I_2_Q70l2=1 if I_2_Q70l==2 |  I_2_Q70l==3 |  I_2_Q70l==4 |  I_2_Q70l==5 |  I_2_Q70l==6 |  I_2_Q70l==7 |  I_2_Q70l==8 

gen I_3_Q70l2=. , after(I_3_Q70l)
replace I_3_Q70l2=0 if I_3_Q70l==1
replace I_3_Q70l2=1 if I_3_Q70l==2 |  I_3_Q70l==3 |  I_3_Q70l==4 |  I_3_Q70l==5 |  I_3_Q70l==6 |  I_3_Q70l==7 |  I_3_Q70l==8 

gen I_4_Q70l2=. , after(I_4_Q70l)
replace I_4_Q70l2=0 if I_4_Q70l==1
replace I_4_Q70l2=1 if I_4_Q70l==2 |  I_4_Q70l==3 |  I_4_Q70l==4 | I_4_Q70l==7 |  I_4_Q70l==8 | I_4_Q70l==14

gen I_5_Q70l2=. , after(I_5_Q70l)
replace I_5_Q70l2=0 if I_5_Q70l==1
replace I_5_Q70l2=1 if I_5_Q70l==2 |  I_5_Q70l==3 

gen I_6_Q70l2=. , after(I_6_Q70l)
replace I_6_Q70l2=0 if I_6_Q70l==1

egen AverageOpposition=rmean(I_1_Q70l2 I_2_Q70l2 I_3_Q70l2 I_4_Q70l2 I_5_Q70l2 I_6_Q70l2)
label variable AverageOpposition "Average amount of opposition partisanship across all discussion partners"
order AverageOpposition, before (Q76a)

***Average Opposition NOT conditional on partisanship
gen I_1_Q70l3=. , after(I_1_Q70l2)
replace I_1_Q70l3=0 if I_1_Q70l==1 | I_1_Q70k==0
replace I_1_Q70l3=1 if I_1_Q70l==2 |  I_1_Q70l==3 |  I_1_Q70l==4 |  I_1_Q70l==5 |  I_1_Q70l==6 |  I_1_Q70l==7 |  I_1_Q70l==8 | I_1_Q70l==9 

gen I_2_Q70l3=. , after(I_2_Q70l2)
replace I_2_Q70l3=0 if I_2_Q70l==1 | I_2_Q70k==0
replace I_2_Q70l3=1 if I_2_Q70l==2 |  I_2_Q70l==3 |  I_2_Q70l==4 |  I_2_Q70l==5 |  I_2_Q70l==6 |  I_2_Q70l==7 |  I_2_Q70l==8 

gen I_3_Q70l3=. , after(I_3_Q70l2)
replace I_3_Q70l3=0 if I_3_Q70l==1 | I_3_Q70k==0
replace I_3_Q70l3=1 if I_3_Q70l==2 |  I_3_Q70l==3 |  I_3_Q70l==4 |  I_3_Q70l==5 |  I_3_Q70l==6 |  I_3_Q70l==7 |  I_3_Q70l==8 

gen I_4_Q70l3=. , after(I_4_Q70l2)
replace I_4_Q70l3=0 if I_4_Q70l==1 | I_4_Q70k==0
replace I_4_Q70l3=1 if I_4_Q70l==2 |  I_4_Q70l==3 |  I_4_Q70l==4 | I_4_Q70l==7 |  I_4_Q70l==8 | I_4_Q70l==14

gen I_5_Q70l3=.  , after(I_5_Q70l2)
replace I_5_Q70l3=0 if I_5_Q70l==1 | I_5_Q70k==0
replace I_5_Q70l3=1 if I_5_Q70l==2 |  I_5_Q70l==3 

gen I_6_Q70l3=.  , after(I_6_Q70l2)
replace I_6_Q70l3=0 if I_6_Q70l==1 | I_6_Q70k==0

egen AverageOpposition1=rmean(I_1_Q70l3 I_2_Q70l3 I_3_Q70l3 I_4_Q70l3 I_5_Q70l3 I_6_Q70l3)
label variable AverageOpposition1 "Average amount of opposition partisanship across all discussion partners"
order AverageOpposition1, before (Q76a)

***Average RP partisanship NOT conditional on partisanship
gen I_1_Q70l4=. , after(I_1_Q70l3)
replace I_1_Q70l4=1 if I_1_Q70l==1 
replace I_1_Q70l4=0 if I_1_Q70k==0 | I_1_Q70l==2 |  I_1_Q70l==3 |  I_1_Q70l==4 |  I_1_Q70l==5 |  I_1_Q70l==6 |  I_1_Q70l==7 |  I_1_Q70l==8 | I_1_Q70l==9 

gen I_2_Q70l4=. , after(I_2_Q70l3)
replace I_2_Q70l4=1 if I_2_Q70l==1 
replace I_2_Q70l4=0 if I_2_Q70l==2 |  I_2_Q70l==3 |  I_2_Q70l==4 |  I_2_Q70l==5 |  I_2_Q70l==6 |  I_2_Q70l==7 |  I_2_Q70l==8 | I_2_Q70k==0

gen I_3_Q70l4=. , after(I_3_Q70l3)
replace I_3_Q70l4=1 if I_3_Q70l==1 
replace I_3_Q70l4=0 if I_3_Q70l==2 |  I_3_Q70l==3 |  I_3_Q70l==4 |  I_3_Q70l==5 |  I_3_Q70l==6 |  I_3_Q70l==7 |  I_3_Q70l==8 | I_3_Q70k==0

gen I_4_Q70l4=. , after(I_4_Q70l3)
replace I_4_Q70l4=1 if I_4_Q70l==1 
replace I_4_Q70l4=0 if I_4_Q70l==2 |  I_4_Q70l==3 |  I_4_Q70l==4 | I_4_Q70l==7 |  I_4_Q70l==8 | I_4_Q70l==14 | I_4_Q70k==0

gen I_5_Q70l4=.  , after(I_5_Q70l3)
replace I_5_Q70l4=1 if I_5_Q70l==1 
replace I_5_Q70l4=0 if I_5_Q70l==2 |  I_5_Q70l==3 | I_5_Q70k==0

gen I_6_Q70l4=.  , after(I_6_Q70l3)
replace I_6_Q70l4=1 if I_6_Q70l==1 
replace I_6_Q70l4=0 if I_6_Q70k==0


egen AverageRP1=rmean(I_1_Q70l4 I_2_Q70l4 I_3_Q70l4 I_4_Q70l4 I_5_Q70l4 I_6_Q70l4)
label variable AverageRP1 "Average amount of ruling party partisanship across all discussion partners"
order AverageRP1, after (AverageOpposition1)

***Relationships that predate partisanship
gen I_1_Q70a_prepartisan=. , after(I_1_Q70a)
replace I_1_Q70a_prepartisan=1 if I_1_Q70a<Q65d
replace I_1_Q70a_prepartisan=0 if I_1_Q70a>Q65d | I_1_Q70a==Q65d
replace I_1_Q70a_prepartisan=. if I_1_Q70a==. | Q65d==.
label define prepartisan 1 "Respondent met discussion partner BEFORE they began supporting a political party" 0 "Respondent met discussion partner AFTER (or at the same time) they began supporting a political party" 
label values I_1_Q70a_prepartisan prepartisan
label variable I_1_Q70a_prepartisan "Respondent met discussion partner #1 after (or at the same time) they began supporting a political party"

gen I_2_Q70a_prepartisan=. , after(I_2_Q70a)
replace I_2_Q70a_prepartisan=1 if I_2_Q70a<Q65d
replace I_2_Q70a_prepartisan=0 if I_2_Q70a>Q65d | I_2_Q70a==Q65d
replace I_2_Q70a_prepartisan=. if I_2_Q70a==. | Q65d==.
label values I_2_Q70a_prepartisan prepartisan
label variable I_2_Q70a_prepartisan "Respondent met discussion partner #2 after (or at the same time) they began supporting a political party"

gen I_3_Q70a_prepartisan=. , after(I_3_Q70a)
replace I_3_Q70a_prepartisan=1 if I_3_Q70a<Q65d
replace I_3_Q70a_prepartisan=0 if I_3_Q70a>Q65d | I_3_Q70a==Q65d
replace I_3_Q70a_prepartisan=. if I_3_Q70a==. | Q65d==.
label values I_3_Q70a_prepartisan prepartisan
label variable I_3_Q70a_prepartisan "Respondent met discussion partner #3 after (or at the same time) they began supporting a political party"

gen I_4_Q70a_prepartisan=. , after(I_4_Q70a)
replace I_4_Q70a_prepartisan=1 if I_4_Q70a<Q65d
replace I_4_Q70a_prepartisan=0 if I_4_Q70a>Q65d | I_4_Q70a==Q65d
replace I_4_Q70a_prepartisan=. if I_4_Q70a==. | Q65d==.
label values I_4_Q70a_prepartisan prepartisan
label variable I_3_Q70a_prepartisan "Respondent met discussion partner #4 after (or at the same time) they began supporting a political party"

gen I_5_Q70a_prepartisan=. , after(I_5_Q70a)
replace I_5_Q70a_prepartisan=1 if I_5_Q70a<Q65d
replace I_5_Q70a_prepartisan=0 if I_5_Q70a>Q65d | I_5_Q70a==Q65d
replace I_5_Q70a_prepartisan=. if I_5_Q70a==. | Q65d==.
label values I_5_Q70a_prepartisan prepartisan
label variable I_3_Q70a_prepartisan "Respondent met discussion partner #5 after (or at the same time) they began supporting a political party"

gen I_6_Q70a_prepartisan=. , after(I_6_Q70a)
replace I_6_Q70a_prepartisan=1 if I_6_Q70a<Q65d
replace I_6_Q70a_prepartisan=0 if I_6_Q70a>Q65d | I_6_Q70a==Q65d
replace I_6_Q70a_prepartisan=. if I_6_Q70a==. | Q65d==.
label values I_6_Q70a_prepartisan prepartisan
label variable I_3_Q70a_prepartisan "Respondent met discussion partner #6 after (or at the same time) they began supporting a political party"

***Ethnic homophily
gen I_1_Q70c_coethnic=0 if I_1_Q70c>=0, after(I_1_Q70c)
replace I_1_Q70c_coethnic=1 if I_1_Q70c==Ethnicity
replace I_1_Q70c_coethnic=. if I_1_Q70c==. | Ethnicity==.
label define I_1_Q70c2 0 "Non-Coethnic" 1 "Coethnic"
label values I_1_Q70c_coethnic I_1_Q70c2
label variable I_1_Q70c_coethnic "Respondent and discussant #1 are from the same ethnic group"

gen I_2_Q70c_coethnic=0 if I_2_Q70c>=0, after(I_2_Q70c)
replace I_2_Q70c_coethnic=1 if I_2_Q70c==Ethnicity
replace I_2_Q70c_coethnic=. if I_2_Q70c==. | Ethnicity==.
label values I_2_Q70c_coethnic I_1_Q70c2
label variable I_2_Q70c_coethnic "Respondent and discussant #2 are from the same ethnic group"

gen I_3_Q70c_coethnic=0 if I_3_Q70c>=0, after(I_3_Q70c)
replace I_3_Q70c_coethnic=1 if I_3_Q70c==Ethnicity
replace I_3_Q70c_coethnic=. if I_3_Q70c==. | Ethnicity==.
label values I_3_Q70c_coethnic I_1_Q70c2
label variable I_3_Q70c_coethnic "Respondent and discussant #3 are from the same ethnic group"

gen I_4_Q70c_coethnic=0 if I_4_Q70c>=0, after(I_4_Q70c)
replace I_4_Q70c_coethnic=1 if I_4_Q70c==Ethnicity
replace I_4_Q70c_coethnic=. if I_4_Q70c==. | Ethnicity==.
label values I_4_Q70c_coethnic I_1_Q70c2
label variable I_4_Q70c_coethnic "Respondent and discussant #4 are from the same ethnic group"

gen I_5_Q70c_coethnic=0 if I_5_Q70c>=0, after(I_5_Q70c)
replace I_5_Q70c_coethnic=1 if I_5_Q70c==Ethnicity
replace I_5_Q70c_coethnic=. if I_5_Q70c==. | Ethnicity==.
label values I_5_Q70c_coethnic I_1_Q70c2
label variable I_5_Q70c_coethnic "Respondent and discussant #5 are from the same ethnic group"

gen I_6_Q70c_coethnic=0 if I_6_Q70c>=0, after(I_6_Q70c)
replace I_6_Q70c_coethnic=1 if I_6_Q70c==Ethnicity
replace I_6_Q70c_coethnic=. if I_6_Q70c==. | Ethnicity==.
label values I_6_Q70c_coethnic I_1_Q70c2
label variable I_6_Q70c_coethnic "Respondent and discussant #6 are from the same ethnic group"

gen I_7_Q70c_coethnic=0 if I_7_Q70c>=0, after(I_7_Q70c)
replace I_7_Q70c_coethnic=1 if I_7_Q70c==Ethnicity
replace I_7_Q70c_coethnic=. if I_7_Q70c==. | Ethnicity==.
label values I_7_Q70c_coethnic I_1_Q70c2
label variable I_7_Q70c_coethnic "Respondent and discussant #7 are from the same ethnic group"

egen EthnicHomophily=rmean(I_1_Q70c_coethnic I_2_Q70c_coethnic I_3_Q70c_coethnic I_4_Q70c_coethnic I_5_Q70c_coethnic I_6_Q70c_coethnic I_7_Q70c_coethnic)
label variable EthnicHomophily "Average coethnicity across all discussion partners"
order EthnicHomophily, before (Q76a)

****SES
factor Q122a Q122b Q122c Q122d Q122e Q122f Q122g Q122h Q122i Q122j Q125 Q126 Q128 Q129 Q130, pcf
rotate
predict SES_Factor
order SES_Factor,  after(Q130)
sum SES_Factor, meanonly
replace SES_Factor=( SES_Factor - r(min) ) / ( r(max)-r(min ) ) * 10

***Combining ethnic groups under 10 respondents
gen Ethnicity2=Ethnicity, after(Ethnicity)
replace Ethnicity2=30 if Ethnicity==2 | Ethnicity==3 | Ethnicity==4 | Ethnicity==7 | Ethnicity==10 | Ethnicity==17 | Ethnicity==18 | Ethnicity==19 | Ethnicity==20 | Ethnicity==21 | Ethnicity==23 | Ethnicity==24 | Ethnicity==25 | Ethnicity==27 | Ethnicity==28 | Ethnicity==29 
label values Ethnicity2 ETHNIEa

*********Survey Weights
***OppositionMP2020 
gen OppositionMP2020=. 
replace OppositionMP2020=0 if Arrondissement==3 | Arrondissement==4 | Arrondissement==5 | Arrondissement==6 | Arrondissement==7 | Arrondissement==9 | Arrondissement==11 | Arrondissement==14 | Arrondissement==16 | Arrondissement==17 | Arrondissement==18 | Arrondissement==22 | Arrondissement==25 | Arrondissement==26 | Arrondissement==27 | Arrondissement==28 | Arrondissement==29 | Arrondissement==30 | Arrondissement==31
replace OppositionMP2020=1 if Arrondissement==1 | Arrondissement==2 | Arrondissement==8 | Arrondissement==10 | Arrondissement==12 | Arrondissement==24 
replace OppositionMP2020=2 if Arrondissement==15 | Arrondissement==19 | Arrondissement==20 | Arrondissement==21 | Arrondissement==23 | Arrondissement==13


label define OppositionMP2020a 0 "RDPC MP" 1 "Split representation" 2 "Opposition MP"
label values OppositionMP2020 OppositionMP2020a
label variable OppositionMP2020 "Department is currently represented by an MP from an opposition party"


gen pweights=.
replace pweights=2.033190994 if Region==1 & OppositionMP2020==0
replace pweights=0.139737916 if Region==1 & OppositionMP2020==2
replace pweights=1.225327762 if Region==2 & OppositionMP2020==0
replace pweights=1.420865739 if Region==2 & OppositionMP2020==1
replace pweights=0.536449224 if Region==3 & OppositionMP2020==0
replace pweights=0.672472428 if Region==3 & OppositionMP2020==1
replace pweights=0.526754396 if Region==3 & OppositionMP2020==2
replace pweights=0.956404796 if Region==4 & OppositionMP2020==0
